<template>
  <div class="department-management">
    <header class="header">
      <span class="title">部门管理</span>
      <el-button type="primary" @click="openDialog">新增部门</el-button>
    </header>

    <!-- 新建部门弹窗 -->
    <el-dialog
        :title="dialogTitle"
        v-model="dialogVisible"
        style="width: 1000px;padding: 40px"
        :before-close="handleClose"
    >
      <el-form label-width="80px" label-position="top">
        <el-row :gutter="10">
          <el-col :span="12">
            <el-form-item label="部门名称" :rules="[{ required: true, message: '部门名称不能为空', trigger: 'blur' }]">
              <el-input placeholder="请输入部门名称" v-model="saveDepartmentForm.departmentName"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="上级部门">
              <el-select placeholder="请选择" v-model="saveDepartmentForm.parentId">
                <el-option v-for="item in departmentArr" :label="item.username" :value="item.id"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <!-- 这是部门描述输入框 -->
        <el-input
            type="textarea"
            placeholder="请输入部门描述"
            v-model="saveDepartmentForm.description"
            :rows="4"
            maxlength="200"
            show-word-limit
        ></el-input>
      </el-form>
      <template #footer>
        <el-button @click="handleClose">取消</el-button>
        <el-button type="primary" @click="saveDepartment">保存</el-button>
      </template>
    </el-dialog>

    <!-- 搜索卡片 -->
    <el-card class="search-card">
      <el-form :inline="true">
        <el-form-item label="部门搜索">
          <el-input placeholder="请输入部门名称" style="width: 220px;" v-model="searchDepartmentForm.departmentName"
                    @keydown.enter="loadDepartmentList"></el-input>
        </el-form-item>
        <el-form-item label="部门详情" style="width: 290px;">
          <el-select placeholder="请选择" v-model="searchDepartmentForm.parentId" @change="loadDepartmentList">
            <el-option v-for="item in departmentArr" :label="item.username" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button @click="resetSearch">重置</el-button>
          <el-button type="primary" @click="loadDepartmentList">查询</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <!-- 部门列表 Card-->
    <el-card>
      <el-table :data="departmentTableData" class="department-table">
        <el-table-column type="index" label="编号" width="80" align="center"/>
        <el-table-column prop="departmentName" label="部门名称" align="center"/>
        <!--        <el-table-column prop="parentId" label="上级部门编号" align="center"/>-->
        <el-table-column prop="parentName" label="上级部门" align="center"/>
        <el-table-column prop="description" label="部门描述" align="center"/>
        <el-table-column label="操作" align="center">
          <template #default="scope">
            <el-button link type="primary" size="small" @click="editDepartment(scope.row.id)">编辑</el-button>
            <el-button link type="danger" size="small" @click="deleteDepartment(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script setup>
import {onMounted, ref} from "vue";
import axios from "@/http";
import {ElMessage} from "element-plus";
import router from "@/router";
import qs from "qs";

const dialogTitle = ref('');
const dialogVisible = ref(false);


// 搜索表单
const searchDepartmentForm = ref({
  id:'',
  departmentName:'',
  parentId:''
});

//部门列表
const departmentArr = ref([
  {username: '研发部', id: 10},
  {username: '市场部', id: 20},
  {username: '运营部', id: 30},
  {username: '销售部', id: 40},
  {username: '财务部', id: 50},
  {username: '行政部', id: 60},
  {username: '其他', id: 70}
]);
// 部门列表
const departmentTableData = ref([]);

// 新建部门表单
const saveDepartmentForm = ref({
  id:'',
  departmentName: '',
  parentId: '',
  description:''
});

// 获取部门列表
const loadDepartmentList = () => {
  const data = qs.stringify(searchDepartmentForm.value);
  console.log("加载部门列表data",data);
  axios.get('/oa/department/list?' + data).then(response => {
    console.log("加载部门列表response", response);
    console.log("data", data);
    if (response.data.code === 20000) {
      departmentTableData.value = response.data.data;
    } else {
      ElMessage.error(response.data.msg);
    }
  });
};
// const loadDepartmentList = () => {
//   axios.get('/oa/department/list', {
//     params: searchDepartmentForm.value // 将参数作为查询参数传递
//   }).then(response => {
//     console.log("加载部门列表response", response);
//     console.log("searchDepartmentForm", searchDepartmentForm.value);
//     if (response.data.code === 20000) {
//       departmentTableData.value = response.data.data;
//     } else {
//       ElMessage.error(response.data.msg);
//     }
//   }).catch(error => {
//     console.error('加载部门列表失败:', error);
//   });
// };



// 重置搜索
const resetSearch = () => {
  console.log("重置搜索列表。。");
  searchDepartmentForm.value ={};
  loadDepartmentList();
};

// 打开对话框
const openDialog = () => {
  saveDepartmentForm.value = {};
  dialogVisible.value = true;
  dialogTitle.value = '添加部门';
};

// 关闭对话框
const handleClose = () => {
  if (confirm('确定要关闭此弹窗?')) {
    dialogVisible.value = false;
    saveDepartmentForm.value = {};
    loadDepartmentList();
  }
};

// 保存部门
const saveDepartment = () => {
  if (saveDepartmentForm.value.id != null) {
    axios.post('/oa/department/admin/edit/', saveDepartmentForm.value).then(response => {
      if (response.data.code === 20000) {
        ElMessage.success('编辑部门成功');
        dialogVisible.value = false;
        loadDepartmentList();
      } else {
        ElMessage.error(response.data.msg);
      }
    });
  } else {
    axios.post('/oa/department/admin/add', saveDepartmentForm.value).then(response => {
      if (response.data.code === 20000) {
        ElMessage.success("保存成功");
        dialogVisible.value = false;
        loadDepartmentList();
      } else {
        ElMessage.error(response.data.msg);
      }
    });
  }
};

// 编辑部门
const editDepartment = (id) => {
  dialogVisible.value = true;
  dialogTitle.value = '编辑部门信息';
  axios.get('/oa/department/detail/' + id).then(response => {
    if (response.data.code == 20000) {
      saveDepartmentForm.value = response.data.data;
    } else {
      ElMessage.error(response.data.msg);
    }
  });
};

// 删除部门
const deleteDepartment = (id) => {
  if (confirm('确定要删除此部门?')) {
    axios.post('/oa/department/admin/delete/' + id).then(response => {
      if (response.data.code === 20000) {
        ElMessage.success("删除成功");
        loadDepartmentList();
      } else {
        ElMessage.error(response.data.msg);
      }
    });
  }
};

onMounted(() => {
  loadDepartmentList();
});
</script>

<style scoped>
.department-management {
  padding: 20px;
  background-color: #f5f7fa;
}

.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 0;
}

.title {
  font-size: 24px;
  font-weight: bold;
  color: #333;
}

.search-card {
  margin: 20px 0;
  padding: 20px;
  background-color: #ffffff;
  border-radius: 5px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}

.department-table {
  border-radius: 5px;
  overflow: hidden;
}

.department-table .el-table th {
  background-color: #eaeaea;
}
</style>
